package com.ldy.biz.shop.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.biz.shop.entity.OrderItem;
import com.ldy.biz.shop.service.OrderItemService;
import com.ldy.core.common.Result;

/**
 * 订单项管理
 */
@RestController
@RequestMapping("/order/item")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class OrderItemController {

	private static final Logger logger = LoggerFactory.getLogger(OrderItemController.class);

	@Autowired
	private OrderItemService orderItemService;

	/**
	 * 列表分页查询
	 * 
	 * @param current
	 * @param size
	 * @param name
	 * @return
	 */
	@PostMapping("/selectPage")
	public Result selectPage(int current, int size, String name) {
		Page<OrderItem> page = new Page<OrderItem>(current, size);
		QueryWrapper<OrderItem> query = new QueryWrapper<>();
		if (StringUtils.hasText(name)) {
			query.like("name", name);
		}
		query.orderByAsc("name");
		return Result.success(orderItemService.page(page, query));
	}

	/**
	 * 列表查询
	 * 
	 * @return
	 */
	@PostMapping("/selectList")
	public Result selectList() {
		return Result.success(orderItemService.list());
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	@PostMapping("/insert")
	public Result insert(OrderItem param) {
		logger.info("method: {}, params:[param: {}]", "insert", JSONObject.toJSONString(param));
		orderItemService.save(param);
		return Result.success();
	}

	/**
	 * 更新
	 * 
	 * @return
	 */
	@PostMapping("/update")
	public Result update(OrderItem param) {
		logger.info("method: {}, params:[app: {}]", "update", JSONObject.toJSONString(param));
		orderItemService.updateById(param);
		return Result.success();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	public Result delete(String id) {
		logger.info("method: {}, params:[id: {}]", "delete", id);
		orderItemService.removeById(id);
		return Result.success();
	}
}
